﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.Generic;
using DiscoveryLogic.DataServices.DataSources;
using System.Windows.Markup;

namespace DiscoveryLogic.UI.Layouts.Controls
{
    public class StatementManager
    {
        #region Private Variables

            private Statement[] statements;
            //   private Hashtable statementsTable;
            private Dictionary<string, object> statementsTable; 
            private Dictionary<string, PageTurnPage> pageDictionary;

        #endregion

        #region Constructors

            /// <summary>
            /// Create a new instance of StatementManager
            /// </summary>
            /// <param name="statements"></param>
            public StatementManager(Statement[] statements)
            {
                this.statements = statements;
                // statementsTable = new Hashtable();
                statementsTable = new Dictionary<string, object>(); 
                pageDictionary = new Dictionary<string, PageTurnPage>();

                // Store the statements indexed by their abbreviation
                for (int i = 0; i < statements.Length; i++)
                {
                    statementsTable[statements[i].Abbreviation] = statements[i];

                }
            }

        #endregion

        #region Public Methods

            /// <summary>
            /// Returns the dictionary that stores the Page (front) indexed
            /// by key
            /// </summary>
            public Dictionary<string, PageTurnPage> PageDictionary
            {
                get
                {
                    return pageDictionary;
                }
            }

            /// <summary>
            /// Returns a statement object given the title of the statement
            /// </summary>
            /// <param name="title"></param>
            /// <returns></returns>
            public Statement GetStatement(string title)
            {
                return (Statement)statementsTable[title];
            }

            /// <summary>
            /// For each statement page, build a PageTurnPage object which
            /// represents a page (front). Creates textblock elements representing
            /// transactions on the statement and puts it on the Canvas of the
            /// PageTurnPage object.
            /// </summary>
            /// <returns>Returns a list of PageTurnPage objects which 
            /// represents the pages (front).</returns>
            public List<PageTurnPage> GetPageTurnPages()
            {
                List<PageTurnPage> pages = new List<PageTurnPage>();

                // For each statement create a new pageturnpage object
                // and set its properties according to the statement object
                foreach (Statement s in statements)
                {
                    PageTurnPage ptp = new PageTurnPage();
                    
                    ptp.Title.Text = s.Title;
                    ptp.Statement = s;

                    int numPages = (s.Transactions.Length - 1) / 15 + 1;

                    // Keeps track of how far down the line of text is placed on the page
                    // After each line is put onto the canvas, this gets incremented
                    double currTop = 74.5;

                    // For each transaction, create textblock objects and place it in the canvas
                    // of the PageTurnPage obj
                    for (int j = 0; j < s.Transactions.Length && j < 15; j++)
                    {
                        // Replace any apostrophes
                        string description = s.Transactions[j].Description;
                        description = description.Replace("'", "");

                        // If the transaction text is too long, then shorten it
                        string trimmedDescription = description.Substring(0, Math.Min(description.Length, 25));
                        if (!trimmedDescription.Equals(description))
                        {
                            trimmedDescription += "...";
                        }

                        string date = s.Transactions[j].Date.ToString("MM/dd");
                        string amount = s.Transactions[j].Amount.ToString();
                        string balance = s.Transactions[j].Balance.ToString();
                        TextBlock tb = XamlLoader<TextBlock>.Create(String.Format(@"<TextBlock xmlns='http://schemas.microsoft.com/client/2007' Width='314' Height='21.5' TextWrapping='NoWrap' FontFamily='Trebuchet MS' FontSize='11' Text='{1}' Canvas.Left='7' Canvas.Top='{0}'/>", currTop, date));
                        ptp.Transactions.Children.Add(tb);
                        tb = XamlLoader<TextBlock>.Create(String.Format(@"<TextBlock xmlns='http://schemas.microsoft.com/client/2007' Width='314' Height='21.5' TextWrapping='NoWrap' FontFamily='Trebuchet MS' FontSize='11' Text='{1}' Canvas.Left='70' Canvas.Top='{0}'/>", currTop, trimmedDescription));
                        ptp.Transactions.Children.Add(tb);
                        tb = XamlLoader<TextBlock>.Create(String.Format(@"<TextBlock xmlns='http://schemas.microsoft.com/client/2007' Width='314' Height='21.5' TextWrapping='NoWrap' FontFamily='Trebuchet MS' FontSize='11' Text='{1}' Canvas.Left='225' Canvas.Top='{0}'/>", currTop, amount));
                        ptp.Transactions.Children.Add(tb);
                        tb = XamlLoader<TextBlock>.Create(String.Format(@"<TextBlock xmlns='http://schemas.microsoft.com/client/2007' Width='314' Height='21.5' TextWrapping='NoWrap' FontFamily='Trebuchet MS' FontSize='11' Text='{1}' Canvas.Left='275' Canvas.Top='{0}'/>", currTop, balance));
                        ptp.Transactions.Children.Add(tb);

                        ptp.InfoTreatmentRequested += new EventHandler(OnInfoTreatmentRequested);

                        currTop += 20;

                    }

                    ptp.PageString.Text = String.Format("{0} of {1}", 1, numPages);

                    // Add this PageTurnPage to the list of pages
                    pages.Add(ptp);
                    // Also add it to the dictionary
                    pageDictionary.Add(s.Abbreviation, ptp);

                    ptp.Subtitle.Text = String.Format("{0} - {1}", s.StartDate.ToString("M"), s.EndDate.ToString("M"));

                    // If there are more transactions than 15, then start creating additional 
                    // pages. Same logic as the block of code above.
                    if (s.Transactions.Length > 15)
                    {
                        int currTran = 15;

                        while (currTran < s.Transactions.Length - 1)
                        {

                            ptp = new PageTurnPage();
                            currTop = 74.5;
                            ptp.Title.Text = s.Title;
                            ptp.Statement = s;

                            // For each transaction, create the line of text and put it on
                            // the canvas
                            for (int j = currTran; j < s.Transactions.Length && j < currTran + 15; j++)
                            {
                                // Replace apostrophes
                                string description = s.Transactions[j].Description;
                                description = description.Replace("'", "");

                                // If transaction text is too long, then shorten it
                                string trimmedDescription = description.Substring(0, Math.Min(description.Length, 25));
                                if (!trimmedDescription.Equals(description))
                                {
                                    trimmedDescription += "...";
                                }

                                string date = s.Transactions[j].Date.ToString("d/MM");
                                string amount = s.Transactions[j].Amount.ToString();
                                string balance = s.Transactions[j].Balance.ToString();
                                TextBlock tb = (TextBlock)XamlReader.Load(String.Format(@"<TextBlock xmlns='http://schemas.microsoft.com/client/2007' Width='314' Height='21.5' TextWrapping='NoWrap' FontFamily='Trebuchet MS' FontSize='11' Text='{1}' Canvas.Left='7' Canvas.Top='{0}'/>", currTop, date));
                                ptp.Transactions.Children.Add(tb);
                                tb = XamlLoader<TextBlock>.Create(String.Format(@"<TextBlock xmlns='http://schemas.microsoft.com/client/2007' Width='314' Height='21.5' TextWrapping='NoWrap' FontFamily='Trebuchet MS' FontSize='11' Text='{1}' Canvas.Left='70' Canvas.Top='{0}'/>", currTop, trimmedDescription));
                                ptp.Transactions.Children.Add(tb);
                                tb = XamlLoader<TextBlock>.Create(String.Format(@"<TextBlock xmlns='http://schemas.microsoft.com/client/2007' Width='314' Height='21.5' TextWrapping='NoWrap' FontFamily='Trebuchet MS' FontSize='11' Text='{1}' Canvas.Left='225' Canvas.Top='{0}'/>", currTop, amount));
                                ptp.Transactions.Children.Add(tb);
                                tb = XamlLoader<TextBlock>.Create(String.Format(@"<TextBlock xmlns='http://schemas.microsoft.com/client/2007' Width='314' Height='21.5' TextWrapping='NoWrap' FontFamily='Trebuchet MS' FontSize='11' Text='{1}' Canvas.Left='275' Canvas.Top='{0}'/>", currTop, balance));
                                ptp.Transactions.Children.Add(tb);

                                ptp.InfoTreatmentRequested += new EventHandler(OnInfoTreatmentRequested);

                                currTop += 20;

                            }

                            currTran += 15;

                            int currentPageNum = currTran / 15;
                            
                            ptp.Subtitle.Text = String.Format("{0} - {1}", s.StartDate.ToString("M"), s.EndDate.ToString("M"));
                            ptp.PageString.Text = String.Format("{0} of {1}", currentPageNum, numPages);

                            // Add page to pages list
                            pages.Add(ptp);


                        }
                    }
                
                }

                // Return list of pages
                return pages;

            }

        #endregion

        #region Event Handlers

            /// <summary>
            /// Raise event when question mark on the statement is clicked
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void OnInfoTreatmentRequested(object sender, EventArgs e)
            {
                InfoTreatmentRequested(sender, e);
            }

        #endregion

        #region Events

            /// <summary>
            /// Event that gets raised when question mark is clicked
            /// </summary>
            public event EventHandler InfoTreatmentRequested;

        #endregion
    }
}
